package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.land;
import cn.com.hfuu.module.role;
import cn.com.hfuu.module.user;
import cn.com.hfuu.service.LandService;
import cn.com.hfuu.service.RoleService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;

@RequestMapping("land")
@Controller
public class LandControl {

	@Autowired
	LandService landser;

	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleservice;

	@RequestMapping("info")
	public String info() {
		return "land/land";
	}

	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit) {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		List<land> landlist = landser.selectbypage(start, limit);
		for (land land : landlist) {
			land.setUser(userService.selectuserbyid(land.getLandFunctionary()));
		}
		page.setRoot(landlist);
		page.setTotal(landser.selectall().size());
		return page;
	}
	
	@RequestMapping("listandroid")
	@ResponseBody
	public List<land> listandroid(HttpServletRequest req){
		HttpSession session=req.getSession();
		user u=(user)session.getAttribute("user");
		List<land> landlist=landser.selectall();
		return landlist;
	}
	
	@RequestMapping("add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		List<role> rolelist = roleservice.selectcyyg();
		List<user> userlist = new ArrayList<user>();
		for (role role : rolelist) {
			userlist.addAll(userService.selectyg(role.getRoleId()));
		}
		mv.setViewName("land/add");
		mv.addObject("userlist", userlist);
		return mv;

	}
	
	@RequestMapping("addad")
	@ResponseBody
	public List<user> userlistad(){
		List<role> rolelist = roleservice.selectcyyg();
		List<user> userlist = new ArrayList<user>();
		for (role role : rolelist) {
			userlist.addAll(userService.selectyg(role.getRoleId()));
		}
		return userlist;
	}

	@RequestMapping("addproc")
	@ResponseBody
	public String addproc(land l) {
		if (landser.selectbyname(l.getLandName())!=null) {
			return "error";
		}
		UUIdUtil id = new UUIdUtil();
		l.setLandId(id.getUuid());
		if (landser.insert(l) > 0) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("addprocad")
	@ResponseBody
	public String addprocad(@RequestBody land l) {
		 if (landser.selectbyname(l.getLandName())!=null) {
			return "error";
		}
		UUIdUtil id = new UUIdUtil();
		l.setLandId(id.getUuid());
		if (landser.insert(l) > 0) {
			return "success";
		}
		return "false";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id) {
		land l=landser.selectbyid(id);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("land/edit");
		List<role> rolelist = roleservice.selectcyyg();
		List<user> userlist = new ArrayList<user>();
		for (role role : rolelist) {
			userlist.addAll(userService.selectyg(role.getRoleId()));
		}
		mv.addObject("userlist", userlist);
		mv.addObject("land", l); 
		return mv;
	}
	
	@RequestMapping("editproc")
	@ResponseBody
	public String editproc(land l) {
		if (landser.exitnameedit(l.getLandName(), l.getLandId()).size()!=0) {
			return "error";
		}
		if (landser.update(l) > 0) {
			return "success";
		}
		return "false";
	}
	
	
	
	@RequestMapping("editprocad")
	@ResponseBody
	public String editprocad(@RequestBody land l) {
		System.out.println(l.toString());
		if (landser.exitnameedit(l.getLandName(), l.getLandId()).size()!=0) {
			return "error";
		}
		if (landser.update(l) > 0) {
			return "success";
		}
		return "false";
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {

		List<land> landlist = landser.selectall();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (land land : landlist) {
			land.setUser(userService.selectuserbyid(land.getLandFunctionary()));
			Map<String, String> map1 = new HashMap<String, String>();
			map1.put("编号", land.getLandId());
			map1.put("茶园名", land.getLandName());
			map1.put("主要品种", land.getLandVariety());
			map1.put("主要负责人", land.getUser().getUserName());
			map1.put("产期（月）", land.getLandTime());
			map1.put("面积（㎡）", land.getLandArea());
			if(land.getLandState()==0){
				map1.put("状态", "关闭");
			}else{
				map1.put("状态", "正常");
			}
			map1.put("地址", land.getLandSite());
			mapList.add(map1);

		}
		String[] title = { "编号", "茶园名", "主要品种", "主要品种","主要负责人","产期（月）","面积（㎡）","状态","地址" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶园信息");
		return new ModelAndView(ve, map);
	}

}
